/**
 * author:wos
 * time:2024-1-4
 * title:删除目录权限
 */

const parseQueryParams = require("../../../utils/parseQueryParams");

async function del(ctx, next) {
    const tableName = "sys_role"
    const tableIndexName = 'roleId'

    const url = ctx.request.url
    const query = Object.assign({}, parseQueryParams(url))

    if (query[tableIndexName] === undefined) {
        global.response(ctx, {
            "status": 200,
            "code": 500,
            "data": {},
            "msg": '参数错误'
        })
    } else {
        try {
            const checkSql = await global.query(`select * from sys_user_role where roleId = ${query[tableIndexName]}`)
            if (checkSql.length > 0) {
                global.response(ctx, {
                    "status": 200,
                    "code": 500,
                    "data": {},
                    "msg": '无法删除此角色，该角色权限被使用。请先删除该角色权限。'
                })
            } else {
                const sql = `delete from ${tableName} where ${tableIndexName} = ${query[tableIndexName]}`
                let rs = await global.query(sql)
                global.response(ctx, {
                    "status": 200,
                    "code": 200,
                    "data": rs,
                    "msg": 'success'
                })
            }
        } catch (err) {
            global.serverError(ctx)
        }
    }
}

module.exports = del